package gui.windows;

import gui.GUIHandler;
import gui.windows.components.DeckPanel;
import gui.windows.components.FileList;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import app.AppGlobals;
import config.LauncherHelper;

@SuppressWarnings("serial")
public class FileManagerReplayTab extends JPanel {
	private FileList fileList;
	private DeckPanel fileDeckPanel;
	public FileManagerReplayTab(){
		this.setLayout(null);
		this.setBounds(0, 0, 951, 421);
		JPanel panel_14 = new JPanel();
		panel_14.setBounds(10, 11, 415, 371);
		this.add(panel_14);
		panel_14.setLayout(null);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 11, 395, 279);
		panel_14.add(scrollPane_2);
		
		fileList = new FileList(AppGlobals.launcherConfig.launcherDir + "/replay",".yrp");
		scrollPane_2.setViewportView(fileList);
		
		JButton btnViewReplays = new JButton("View Replays");
		btnViewReplays.setBounds(221, 335, 99, 23);
		panel_14.add(btnViewReplays);
		btnViewReplays.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LauncherHelper.runGame("-r");
			}
		});
		
		JButton btnOpenFolder = new JButton("Open Folder");
		btnOpenFolder.setBounds(112, 335, 99, 23);
		panel_14.add(btnOpenFolder);
		btnOpenFolder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (Desktop.isDesktopSupported()) {
					try {
						Desktop.getDesktop().open(new File(AppGlobals.launcherConfig.launcherDir + "replay/"));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
		JButton btnRename = new JButton("Rename");
		btnRename.setBounds(276, 301, 99, 23);
		panel_14.add(btnRename);
		btnRename.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(fileList.getSelectedIndex() != -1){
					File file = fileList.getSelectedValue();
					String str = GUIHandler.launcherRenameReplay();
					File renameFile = new File(file.getParentFile() + "/" + str + ".yrp");
					if(!renameFile.exists() && !str.isEmpty()){
						if(file.renameTo(renameFile)){
							DefaultListModel<File> listModel = (DefaultListModel<File>)fileList.getModel();
							listModel.removeElement(file);
							listModel.addElement(renameFile);
						}
					}						
				}
			}
		});
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.setBounds(167, 301, 99, 23);
		panel_14.add(btnDelete);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(fileList.getSelectedIndex() != -1){
					File file = fileList.getSelectedValue();
					if(file.delete()){
						DefaultListModel<File> listModel = (DefaultListModel<File>)fileList.getModel();
						listModel.removeElement(file);
					}
						
				}
			}
		});
		
		JButton btnImport = new JButton("Import");
		btnImport.setBounds(58, 301, 99, 23);
		panel_14.add(btnImport);
		btnImport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {                
                File[] replays = GUIHandler.launcherImportReplays();
                for(File replay: replays){
                	File newfile = new File(AppGlobals.launcherConfig.launcherDir + "/replay/" + replay.getName());
                	if(newfile.exists())
                		continue;
                	if(replay.renameTo(newfile)){
						DefaultListModel<File> listModel = (DefaultListModel<File>)fileList.getModel();
						listModel.addElement(newfile);
                	}
                }
			}
		});
		
		fileDeckPanel = new DeckPanel();
		
		fileDeckPanel.setBounds(429, 22, 476, 348);
		this.add(fileDeckPanel);		
		fileList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if(e.getValueIsAdjusting())
                    return;
                else{
                	File deck = fileList.getSelectedValue();
                	if(fileDeckPanel != null && deck != null)
                		fileDeckPanel.loadDeck(deck);
                }
            }
          });
	}
}
